﻿/*
Medion Info Display

Copyright 2010 Wolf Posdorfer,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace MD8800.util
{
    /// <summary>
    /// Eine Klasse die zum verbinden und kommunizieren mit dem LCD
    /// Display dient
    /// </summary>
    public class PortController
    {

        private int _baudRate = 9600;
        private int _dataBits = 8;
        private SerialPort comPort = new SerialPort();
        private String _portName = "COM2";
        /// <summary>
        /// erstellt einen neuen PortController
        /// </summary>
        public PortController()
        {
        }
        /// <summary>
        /// Öffnet den Port, falls möglich
        /// </summary>
        /// <returns>true,wenn der Port geöffnet werden konnte,sonst false</returns>
        public bool openport()
        {
            try
            {
                if (comPort.IsOpen == true) comPort.Close();
                comPort.BaudRate = _baudRate;    //BaudRate
                comPort.DataBits = _dataBits;    //DataBits
                comPort.PortName = _portName;   //PortName
                comPort.Open();
                //System.Console.WriteLine("port open");
                return true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                //DisplayData(MessageType.Error, ex.Message);
                return false;
            }
        }
        /// <summary>
        /// Schreibt einen String auf das Display
        /// </summary>
        /// <param name="s">String</param>
        public void write(String s)
        {

            if (comPort.IsOpen)
            {
                comPort.Write(s);
            }

        }
        /// <summary>
        /// Schließt den Port
        /// </summary>
        public void closePort()
        {

            comPort.Close();
        }
        /// <summary>
        /// Überprüft ob der Port offen ist
        /// </summary>
        /// <returns>true wenn offen, sonst false</returns>
        public bool isOpen()
        {
            return comPort.IsOpen;
        }
        /// <summary>
        /// Löscht den bildschirm von allen Sachen
        /// optional wird die Uhr angezeigt nach dem Löschen
        /// </summary>
        /// <param name="settime">wenn true, wird die Uhr nach dem Löschen angezeigt</param>
        public void clearScreen(bool settime)
        {

            if (comPort.IsOpen)
            {
                comPort.Write(opcodes.CLEAR);
                if (settime)
                {
                    comPort.Write(opcodes.SET_TIME + Time.gibZeit());
                }
            }

        }

        public void clearText()
        {
            if (comPort.IsOpen)
            {
                comPort.Write(opcodes.CLEAR_TEXT);
            }
        }

        /// <summary>
        /// drawborder malt die angegebenen Ränder
        /// 0=HDD;
        /// 1=Movie;
        /// 2=Graphic;
        /// 3=Mail;
        /// 4=Volume
        /// </summary>
        public void drawborder(int border, int brightness)
        {
            String s = "";
            switch (border)
            {
                case 0: s = opcodes.HDD_BORDER;
                    break;
                case 1: s = opcodes.MOVIE_BORDER;
                    break;
                case 2: s = opcodes.GRAPHIC_BORDER;
                    break;
                case 3: s = opcodes.MAIL_BORDER;
                    break;
                case 4: s = opcodes.VOLUME_BORDER;
                    break;
                default:
                    break;
            }
            switch (brightness)
            {
                case 0: s += opcodes.BRIGHT_0;
                    break;
                case 1: s += opcodes.BRIGHT_1;
                    break;
                case 2: s += opcodes.BRIGHT_2;
                    break;
                case 3: s += opcodes.BRIGHT_3;
                    break;
                default:
                    break;
            }
            write(s);
        }

        /// <summary>
        /// Zentriert den String auf dem Display
        /// </summary>
        /// <param name="str">String</param>
        /// <returns>zentrierten String</returns>
        public String centerString(String str)
        {
            String s = str;

            while (str.Length < 15)
            {
                str = " " + str + " ";
            }
            if (str.Length == 15)
            {
                str += " ";
            }
            return str;
        }

    }
}